//
//  SiteView.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-1.
//
//

#import "SiteView.h"
#import "DeviceUtil.h"

@implementation SiteView
{
    id<paySiteViewDelegate> _paydelegate;
    id<typeSiteViewDelegate> _typedelegate;
    
    UISegmentedControl *_segment;
    SitePayView *_payView;
    SiteTypeView *_typeView;
    
    BOOL _segFlag;
    
    NSString *_type;
}

- (void)dealloc
{
    [_segment release];
    [_payView release];
    [_typeView release];
    [_type release];
    
    _segment = nil;
    _payView = nil;
    _typeView = nil;
    _type = nil;
    
    [super dealloc];
    
}

- (id)initWithFrame:(CGRect)frame andDelegate:(id<paySiteViewDelegate>)paydelegate typeDelegate:(id<typeSiteViewDelegate>)typedelegate
{
    _paydelegate = paydelegate;
    _typedelegate = typedelegate;
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        _type =  [[NSString alloc]init];
        //初始化视图
        [self init_view];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


//初始化视图
- (void)init_view
{
//    [self initPayView];
//    [self initTypeView];
//    
//    [self initSegmentCtrl];
//    [self addPayView];
    
    SiteManager *sitemanager = [SiteManager shareManager];
    sitemanager.delegate = self;
    [sitemanager searchAllSiteInfo:@"1"];
    
    [self initTypeView];
    [self addTypeView];
}

//初始化SegmentCtrl
- (void)initSegmentCtrl
{
    _segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], 30)];
    
    [_segment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [_segment setBackgroundColor:[UIColor lightGrayColor]];
    [_segment insertSegmentWithTitle:@"公园主题" atIndex:0 animated:NO];
    [_segment insertSegmentWithTitle:@"其它类别" atIndex:1 animated:NO];
    
    [_segment addTarget:self action:@selector(onTabSelected:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:_segment];
}

-(void)onTabSelected:(id)sender
{
    UISegmentedControl *control = (UISegmentedControl *)sender;
    
    switch (control.selectedSegmentIndex) {
        case 0:
            [self addPayView];
            [self removeTypeView];
            break;
        case 1:
            [self selectSiteType];
            break;
        default:
            break;
    }
    
}

//选择景区类别
- (void)selectSiteType
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"选择景区类别"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"确定"
                                  otherButtonTitles:@"公园主题", @"海滨飘流", @"温泉度假", @"自然生态", @"名胜古迹",@"园林博览",
                                  @"休闲娱乐", @"运动拓展", @"度假酒店", @"自驾游套票", @"直通车套票",@"自助游套票",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        return;
    }else if (buttonIndex >= 8) {
        [self getSiteView:[NSString stringWithFormat:@"%d", buttonIndex]];
    }else if(buttonIndex == 9) {
        [self getSiteView:@"A"];
    }else if(buttonIndex == 10) {
        [self getSiteView:@"B"];
    }else if(buttonIndex == 11) {
        [self getSiteView:@"C"];
    }else{
        [self getSiteView:@"D"];
    }
}

//付款方式
- (void)initPayView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _payView = [[SitePayView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height - 90) andDelegate:_paydelegate];
}
- (void)addPayView
{
    if (_payView)
    {
        _segFlag = 0;
        [self addSubview:_payView];
    }
}
- (void)removePayView
{
    if (_payView)
    {
        [_payView removeFromSuperview];
    }
}

//景区类别
- (void)initTypeView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _typeView = [[SiteTypeView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height - 90) andDelegate:_typedelegate];
}
- (void)addTypeView
{
    if (_typeView)
    {
        _segFlag = 0;
        [self addSubview:_typeView];
    }
}
- (void)removeTypeView
{
    if (_typeView)
    {
        [_typeView removeFromSuperview];
    }
}

//调用景区view
- (void)getSiteView:(NSString*)type
{
    _type = type;
    SiteManager *sitemanager = [SiteManager shareManager];
    sitemanager.delegate = self;
    [sitemanager searchAllSiteInfo:type];

    [self removePayView];
    [self addTypeView];
}

#pragma networking
- (void)qryAllSiteInfo:(BOOL)isSuccess
           AllSiteInfo:(NSMutableArray*)allSiteInfo
       TicketInfoArray:(NSMutableArray*)ticketInfoArray
{
    _typeView.allSiteInfoArray = allSiteInfo;
    _typeView.siteTicketInfoArray = ticketInfoArray;
    [_typeView reloadData];
    
    [self removePayView];
    [self addTypeView];
}

@end



